
from typing import List, Dict, Union
from .column import ColumnInfo, PrimaryKeyInfo
import tablestore
import enum

class OperatorType(enum.Enum):
    EQUAL = 0
    NOT_EQUAL = 1
    LARGER = 2,
    LESS = 3,
    BETWEEN = 4,
    IN = 5,

class Filter():
    def __init__(self, column: Union[ColumnInfo, PrimaryKeyInfo], value, operator_type: OperatorType, query_operators: List) -> None:
        self.column = column
        self.value = value
        self.operators = query_operators
        self.operator_type = operator_type


    def is_pk(self) -> bool:
        if isinstance(self.column, PrimaryKeyInfo):
            return True
        else:
            return False

    @classmethod
    def is_(cls, column: Union[ColumnInfo, PrimaryKeyInfo], value: Union[str, int, float]):
        if isinstance(column, PrimaryKeyInfo):
            return Filter(column=column, value=value, operator_type=OperatorType.EQUAL, query_operators=[(column.name, value), (column.name, value)]) 
        elif isinstance(column, ColumnInfo):
            return Filter(column=column, value=value, operator_type=OperatorType.EQUAL, query_operators=[tablestore.SingleColumnCondition(column.name, value, tablestore.ComparatorType.EQUAL)])
        
    @classmethod
    def not_(cls, column: ColumnInfo, value: Union[str, int, float]):
            return Filter(column=column, value=value, operator_type=OperatorType.NOT_EQUAL, query_operators=[tablestore.SingleColumnCondition(column.name, value, tablestore.ComparatorType.NOT_EQUAL)])

    @classmethod
    def in_(cls, column: ColumnInfo, value: List[Union[str, int, float]]):
        return Filter(column=column, value=value, operator_type=OperatorType.IN, query_operators=[])

    @classmethod
    def between_(cls, column: Union[ColumnInfo, PrimaryKeyInfo], min, max):
        if isinstance(column, PrimaryKeyInfo):
            return Filter(column=column, value=(min, max), operator_type=OperatorType.BETWEEN, query_operators=[(column.name, min), (column.name, max)])
        elif isinstance(column, ColumnInfo):
            return Filter(column=column, value=(min, max), operator_type=OperatorType.BETWEEN, query_operators=[
                tablestore.SingleColumnCondition(column.name, min, tablestore.ComparatorType.GREATER_THAN),
                tablestore.SingleColumnCondition(column.name, max, tablestore.ComparatorType.LESS_THAN)
                ])
        
    @classmethod
    def larger_(cls, column: Union[ColumnInfo, PrimaryKeyInfo], value):
        if isinstance(column, PrimaryKeyInfo):
            return Filter(column=column, value=value, operator_type=OperatorType.LARGER, query_operators=[(column.name, value), (column.name, tablestore.INF_MAX)])
        elif isinstance(column, ColumnInfo):
            return Filter(column=column, value=value,  operator_type=OperatorType.LARGER, query_operators=[tablestore.SingleColumnCondition(column.name, value, tablestore.ComparatorType.GREATER_THAN) ])
        
    @classmethod
    def less_(cls, column: Union[ColumnInfo, PrimaryKeyInfo], value):
        if isinstance(column, PrimaryKeyInfo):
            return Filter(column=column, value=value, operator_type=OperatorType.LESS, query_operators=[(column.name, value), (tablestore.INF_MIN, column.name)])
        elif isinstance(column, ColumnInfo):
            return Filter(column=column, value=value, operator_type=OperatorType.LESS, query_operators=[tablestore.SingleColumnCondition(column.name, value, tablestore.ComparatorType.LESS_THAN) ])
    
is_ = Filter.is_
not_ = Filter.not_
between_ = Filter.between_
larger_ = Filter.larger_
less_ = Filter.less_